const { ccclass, property } = cc._decorator;
declare let i18n,DBManager,dbh;
import BaseView from "../../component/BaseView";
import UIHelper = require("../../component/UIHelper");
import PopUIHelper = require("../../utils/PopUIHelper");
import Utils = require("../../utils/Utils");
//帮女孩坐下  关卡8
@ccclass
export default class DeputySitView extends BaseView {
    @property(cc.Label)
    lblStep: cc.Label = null;
    @property(cc.Label)
    lblDes: cc.Label = null;

    @property(cc.Node)
    paiziNode: cc.Node = null;
    @property(cc.Node)
    duziNode: cc.Node = null;

    @property(cc.Node)
    pengNode1: cc.Node = null;
    @property(cc.Node)
    pengNode2: cc.Node = null;

    @property(sp.Skeleton)
    spine:sp.Skeleton = null;
    @property(sp.Skeleton)
    paiziSpine:sp.Skeleton = null;

    //恭喜获得
    @property(cc.Node)
    endNode: cc.Node = null;

    _done1 = false;
    _done2 = false;
    _selectPos1 = cc.v2(-170,-480);
    _selectPos2 = cc.v2(170,-480);
    _isStart = false;
    onLoad() {
        Utils.audioManager.playBGM("huazhuang/deputyGame/deputyGameBg");
        this.paiziNode.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.paiziNode.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
        this.paiziNode.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.duziNode.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove1, this);
        this.duziNode.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd1, this);
        this.duziNode.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd1, this);
        //@ts-ignore
        this._isStart = this.node.openParam;
        this.lblStep.string = i18n.t("DEPUTY_GAME_STEP_TITLE8");
        this.lblDes.string = i18n.t("DEPUTY_GAME_STEP_DES8");
    };
    onClose(){
        Utils.audioManager.playDefaultBGM();
    };
    onTouchMove(event){
        if(this._done1)
            return;
        let worldPoint = event.touch.getLocation();
        let localPoint = this.paiziNode.parent.convertToNodeSpaceAR(worldPoint);
        this.paiziNode.x = localPoint.x;
        this.paiziNode.y = localPoint.y;
        let box = this.pengNode1.getBoundingBox();
        let boneBox = this.paiziNode.getBoundingBox();
        if(boneBox.intersects(box)){
            this._done1 = true;
            this.paiziNode.active = false;
            Utils.audioManager.playSound("huazhuang/deputyGame/step_8_1");
            this.paiziSpine.node.active = true;
            this.paiziSpine.setAnimation(0,"animation",false);
            this.paiziSpine.setCompleteListener(()=>{
                this.checkDone();
            });
        }
    };
    onTouchEnd(event){
        if(this._done1)
            return;
        this.paiziNode.x = this._selectPos1.x;
        this.paiziNode.y = this._selectPos1.y;
    };
    onTouchMove1(event){
        if(this._done2)
            return;
        let worldPoint = event.touch.getLocation();
        let localPoint = this.duziNode.parent.convertToNodeSpaceAR(worldPoint);
        this.duziNode.x = localPoint.x;
        this.duziNode.y = localPoint.y;
        let box = this.pengNode2.getBoundingBox();
        let boneBox = this.duziNode.getBoundingBox();
        if(boneBox.intersects(box)){
            this._done2 = true;
            this.duziNode.active = false;
            Utils.audioManager.playSound("huazhuang/deputyGame/step_8_2");
            this.spine.setAnimation(0,"02",false);
            this.spine.setCompleteListener(()=>{
                this.spine.setAnimation(0,"03",true);
                this.spine.setCompleteListener(null);
                this.checkDone();
            });
        }
    };
    onTouchEnd1(event){
        if(this._done2)
            return;
        this.duziNode.x = this._selectPos2.x;
        this.duziNode.y = this._selectPos2.y;
    };
    
    onClickEnd(isSkip){
        let next = this.params.next && this.params.next.shift();
        if (next) {
            UIHelper.openUI(next, {
                next: this.params.next
            })
            this.node.destroy();
        }
    };

    checkDone(){
        if(this._done1 && this._done2){
            this.spine.setAnimation(0,"04",false);
            this.spine.setCompleteListener(()=>{
                this.endNode.active = true;
                Utils.audioManager.playSound("huazhuang/deputyGame/pass");
            });
        }
    };
    
    onClickSkip(){
        this.onClickEnd(true);
    };

    //提示
    onClickTips(){
        PopUIHelper.helper.showNewConfirmMsg(i18n.t("DEPUTY_GAME_TIPS8"));
    };

    
    
}
